package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketFootprint;
import com.cskaoyan.market.service.MarketFootPrintService;
import com.cskaoyan.market.service.MarketFootPrintServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: YaLiFeng
 * @description: TODO
 * @date: 2024/5/7 17:03
 */
@WebServlet("/admin/footprint/*")
public class AdminFootPrintController extends HttpServlet {
    private MarketFootPrintService printService=new MarketFootPrintServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI=req.getRequestURI();
        String op=requestURI.replace(req.getContextPath()+"/admin/footprint/","");
        System.out.println("op = " + op);
        if("list".equals(op)){
            list(req,resp);
        }
        


    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String limitParam=req.getParameter("limit");
        String pageParam=req.getParameter("page");
        String addTimeParam=req.getParameter("addTime");
        String deletedParam=req.getParameter("deleted");
        String footIdParam=req.getParameter("id");
        String updateTimeParam=req.getParameter("updateTime");
        String userIdParam=req.getParameter("userId");
        String goodIdParam=req.getParameter("goodsId");
        String sort=req.getParameter("sort");
        String order=req.getParameter("order");

        Integer limit=null;
        Integer page=null;
        try{
            page=Integer.parseInt(pageParam);
            limit=Integer.parseInt(limitParam);

        }catch(NumberFormatException e){
            Object object= ResponseUtil.badArgument();
            String s=JacksonUtil.getObjectMapper().writeValueAsString(object);
            return;
        }
        List<MarketFootprint> marketFootprintList=printService.list(limit,page,order,sort,userIdParam,goodIdParam);
        Object o=ResponseUtil.okList(marketFootprintList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));



    }
}
